home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / print / gs261sr1.zip / GSIMAGE.C < prev    next >
C/C++ Source or Header  |  1993-05-12  |  14KB  |  423 lines

  1. /* Copyright (C) 1989, 1992 Aladdin Enterprises.  All rights reserved.
  2.  
  3. This file is part of Ghostscript.
  4.  
  5. Ghostscript is distributed in the hope that it will be useful, but
  6. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  7. to anyone for the consequences of using it or for whether it serves any
  8. particular purpose or works at all, unless he says so in writing.  Refer
  9. to the Ghostscript General Public License for full details.
  10.  
  11. Everyone is granted permission to copy, modify and redistribute
  12. Ghostscript, but only under the conditions described in the Ghostscript
  13. General Public License.  A copy of this license is supposed to have been
  14. given to you along with Ghostscript so you can know your rights and
  15. responsibilities.  It should be in a file named COPYING.  Among other
  16. things, the copyright notice and this notice must be preserved on all
  17. copies.  */
  18.  
  19. /* gsimage.c */
  20. /* Image setup procedures for Ghostscript library */
  21. #include "gx.h"
  22. #include "memory_.h"
  23. #include "gpcheck.h"
  24. #include "gserrors.h"
  25. #include "gxfixed.h"
  26. #include "gxarith.h"
  27. #include "gxmatrix.h"
  28. #include "gspaint.h"
  29. #include "gzstate.h"
  30. #include "gzdevice.h"            /* requires gsstate.h */
  31. #include "gzcolor.h"            /* requires gxdevice.h */
  32. #include "gzpath.h"
  33. #include "gxcolor.h"
  34. #include "gxcpath.h"
  35. #include "gxdevmem.h"
  36. #include "gximage.h"
  37.  
  38. /* Exported size of enumerator */
  39. const uint gs_image_enum_sizeof = sizeof(gs_image_enum);
  40.  
  41. /* Forward declarations */
  42. private void image_init_map(P3(byte *, int, const float *));
  43. private int image_init(P9(gs_image_enum *, int, int, int,
  44.   int, int, gs_matrix *, gs_state *, fixed));
  45. /* Procedures for unpacking the input data into 8 bits/sample. */
  46. extern iunpack_proc(image_unpack_1);
  47. extern iunpack_proc(image_unpack_1_spread);
  48. extern iunpack_proc(image_unpack_2);
  49. extern iunpack_proc(image_unpack_2_spread);
  50. extern iunpack_proc(image_unpack_4);
  51. extern iunpack_proc(image_unpack_8);
  52. extern iunpack_proc(image_unpack_8_spread);
  53. extern iunpack_proc(image_unpack_12);
  54. /* The image_render procedures work on fully expanded, complete rows. */
  55. /* These take a height argument, which is an integer > 0; */
  56. /* they return a negative code, or the number of */
  57. /* rows actually processed (which may be less than the height). */
  58. extern irender_proc(image_render_skip);
  59. extern irender_proc(image_render_simple);
  60. extern irender_proc(image_render_mono);
  61. extern irender_proc(image_render_color);
  62.  
  63. /* Standard mask tables for spreading input data. */
  64. /* Note that the mask tables depend on the end-orientation of the CPU. */
  65. /* We can't simply define them as byte arrays, because */
  66. /* they might not wind up properly long- or short-aligned. */
  67. #define map4tox(z,a,b,c,d)\
  68.     z, z^a, z^b, z^(a+b),\
  69.     z^c, z^(a+c), z^(b+c), z^(a+b+c),\
  70.     z^d, z^(a+d), z^(b+d), z^(a+b+d),\
  71.     z^(c+d), z^(a+c+d), z^(b+c+d), z^(a+b+c+d)
  72. #if arch_is_big_endian
  73. private const unsigned long map_4x1_to_32[16] =
  74.    {    map4tox(0L, 0xffL, 0xff00L, 0xff0000L, 0xff000000L)    };
  75. private const unsigned long map_4x1_to_32_invert[16] =
  76.    {    map4tox(0xffffffffL, 0xffL, 0xff00L, 0xff0000L, 0xff000000L)    };
  77. #else                    /* !arch_is_big_endian */
  78. private const unsigned long map_4x1_to_32[16] =
  79.    {    map4tox(0L, 0xff000000L, 0xff0000L, 0xff00L, 0xffL)    };
  80. private const unsigned long map_4x1_to_32_invert[16] =
  81.    {    map4tox(0xffffffffL, 0xff000000L, 0xff0000L, 0xff00L, 0xffL)    };
  82. #endif
  83.  
  84. /* Start processing an image */
  85. int
  86. gs_image_init(gs_image_enum *penum, gs_state *pgs,
  87.   int width, int height, int bps,
  88.   int spread, const gs_color_space *pcs, const float *decode /* [spp*2] */,
  89.   gs_matrix *pmat)
  90. {    const gs_color_space_type *pcst = pcs->type;
  91.     int spp = pcst->num_components;
  92.     int ci;
  93.     int device_color = 0;
  94.     static const float default_decode[8] =
  95.         { 0.0, 1.0, 0.0, 1.0, 0.0, 1.0, 0.0, 1.0 };
  96.     if ( pgs->in_cachedevice )
  97.         return_error(gs_error_undefined);
  98.     if ( spp < 0 )            /* Pattern not allowed */
  99.         return_error(gs_error_rangecheck);
  100.     if ( decode == 0 )
  101.         decode = default_decode;
  102.     switch ( pcst->index )
  103.     {
  104.     case gs_color_space_index_DeviceGray:
  105.     case gs_color_space_index_DeviceRGB:
  106.     case gs_color_space_index_DeviceCMYK:
  107.         device_color = 1;
  108.     }
  109.     spread = (spread ? spp : 1);
  110.  
  111.     if ( spp == 1 )
  112.     {    /* Initialize the color table */
  113. #define chtl(i)\
  114.   penum->dev_colors[i].halftone_level
  115.         switch ( bps )
  116.            {
  117.         default:
  118.            {    register gx_device_color *pcht =
  119.                &penum->dev_colors[0];
  120.             register int n = 64;
  121.             do
  122.                {    pcht[0].halftone_level =
  123.                   pcht[1].halftone_level =
  124.                   pcht[2].halftone_level =
  125.                   pcht[3].halftone_level = -1;
  126.                 pcht += 4;
  127.                }
  128.             while ( --n > 0 );
  129.             break;
  130.            }
  131.         case 4:
  132.             chtl(17) = chtl(2*17) = chtl(3*17) =
  133.               chtl(4*17) = chtl(6*17) = chtl(7*17) =
  134.               chtl(8*17) = chtl(9*17) = chtl(11*17) =
  135.               chtl(12*17) = chtl(13*17) = chtl(14*17) = -1;
  136.             /* falls through */
  137.         case 2:
  138.             chtl(5*17) = chtl(10*17) = -1;
  139.         case 1:
  140.             ;
  141.            }
  142. #undef chtl
  143.     }
  144.  
  145.     /* Initialize the maps from samples to intensities. */
  146.  
  147.     for ( ci = 0; ci < spp; ci++ )
  148.     {    sample_map *pmap = &penum->map[ci];
  149.  
  150.         /* If the decoding is [0 1] or [1 0], we can fold it */
  151.         /* into the expansion of the sample values; */
  152.         /* otherwise, we have to use the floating point method. */
  153.  
  154.         const float *this_decode = &decode[ci * 2];
  155.         const float *map_decode;    /* decoding used to */
  156.                 /* construct the expansion map */
  157.  
  158.         const float *real_decode;    /* decoding for */
  159.                 /* expanded samples */
  160.  
  161.         int no_decode;
  162.  
  163.         map_decode = real_decode = this_decode;
  164.         if ( map_decode[0] == 0.0 && map_decode[1] == 1.0 )
  165.             no_decode = 1;
  166.         else if ( map_decode[0] == 1.0 && map_decode[1] == 0.0 )
  167.             no_decode = 1,
  168.             real_decode = default_decode;
  169.         else
  170.             no_decode = 0,
  171.             device_color = 0,
  172.             map_decode = default_decode;
  173.         if ( bps > 2 || spread != 1 )
  174.             image_init_map(&pmap->table.lookup8[0], 1 << bps,
  175.                        map_decode);
  176.         else
  177.         {    /* The map index encompasses more than one pixel. */
  178.             byte map[4];
  179.             register int i;
  180.             image_init_map(&map[0], 1 << bps, map_decode);
  181.             switch ( bps )
  182.             {
  183.             case 1:
  184.             {    register ulong *p = &pmap->table.lookup4x1to32[0];
  185.                 if ( map[0] == 0 && map[1] == 0xff )
  186.                     memcpy((byte *)p, map_4x1_to_32, 16 * 4);
  187.                 else if ( map[0] == 0xff && map[1] == 0 )
  188.                     memcpy((byte *)p, map_4x1_to_32_invert, 16 * 4);
  189.                 else
  190.                   for ( i = 0; i < 16; i++, p++ )
  191.                     ((byte *)p)[0] = map[i >> 3],
  192.                     ((byte *)p)[1] = map[(i >> 2) & 1],
  193.                     ((byte *)p)[2] = map[(i >> 1) & 1],
  194.                     ((byte *)p)[3] = map[i & 1];
  195.             }    break;
  196.             case 2:
  197.             {    register ushort *p = &pmap->table.lookup2x2to16[0];
  198.                 for ( i = 0; i < 16; i++, p++ )
  199.                     ((byte *)p)[0] = map[i >> 2],
  200.                     ((byte *)p)[1] = map[i & 3];
  201.             }    break;
  202.             }
  203.         }
  204.         pmap->decode_base /* = decode_lookup[0] */ = real_decode[0];
  205.         pmap->decode_factor = (real_decode[1] - real_decode[0]) / 255.0;
  206.         pmap->decode_max /* = decode_lookup[15] */ = real_decode[1];
  207.         if ( no_decode )
  208.             pmap->decoding = sd_none;
  209.         else if ( bps <= 4 )
  210.         {    static const int steps[] = { 0, 15, 5, 0, 1 };
  211.             int step = steps[bps];
  212.             int i;
  213.             pmap->decoding = sd_lookup;
  214.             for ( i = 15 - step; i > 0; i -= step )
  215.               pmap->decode_lookup[i] = pmap->decode_base +
  216.                 i * (255.0 / 15) * pmap->decode_factor;
  217.         }
  218.         else
  219.             pmap->decoding = sd_compute;
  220.         if ( spp == 1 )        /* and ci == 0 */
  221.         {    /* Pre-map entries 0 and 255. */
  222.             gs_client_color cc;
  223.             cc.paint.values[0] = real_decode[0];
  224.             (*pcst->remap_color)(&cc, pcs, &penum->icolor0, pgs);
  225.             cc.paint.values[0] = real_decode[1];
  226.             (*pcst->remap_color)(&cc, pcs, &penum->icolor1, pgs);
  227.         }
  228.     }
  229.  
  230.     penum->masked = 0;
  231.     penum->device_color = device_color;
  232.     return image_init(penum, width, height, bps, spread, spp,
  233.               pmat, pgs, (fixed)0);
  234. }
  235. /* Construct a mapping table for sample values. */
  236. /* map_size is 2, 4, 16, or 256.  Note that 255 % (map_size - 1) == 0. */
  237. private void
  238. image_init_map(byte *map, int map_size, const float *decode)
  239. {    float min_v = decode[0], max_v = decode[1];
  240.     byte *limit = map + map_siz